
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Appointment table passing nullable parameters
-- Table Comment: Đặt lịch hẹn
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Appointment_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@LocalId nvarchar (20)  = null ,

	@FirstName nvarchar (50)  = null ,

	@LastName nvarchar (50)  = null ,

	@FromTime datetime   = null ,

	@EndTime datetime   = null ,

	@Description nvarchar (500)  = null ,

	@Doctor nvarchar (100)  = null ,

	@ParentId varchar (15)  = null ,

	@AppointmentType varchar (20)  = null ,

	@CreatedDate datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [LocalId]
	, [FirstName]
	, [LastName]
	, [FromTime]
	, [EndTime]
	, [Description]
	, [Doctor]
	, [ParentId]
	, [AppointmentType]
	, [CreatedDate]
    FROM
	[dbo].[Appointment]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([LocalId] = @LocalId OR @LocalId IS NULL)
	AND ([FirstName] = @FirstName OR @FirstName IS NULL)
	AND ([LastName] = @LastName OR @LastName IS NULL)
	AND ([FromTime] = @FromTime OR @FromTime IS NULL)
	AND ([EndTime] = @EndTime OR @EndTime IS NULL)
	AND ([Description] = @Description OR @Description IS NULL)
	AND ([Doctor] = @Doctor OR @Doctor IS NULL)
	AND ([ParentId] = @ParentId OR @ParentId IS NULL)
	AND ([AppointmentType] = @AppointmentType OR @AppointmentType IS NULL)
	AND ([CreatedDate] = @CreatedDate OR @CreatedDate IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [LocalId]
	, [FirstName]
	, [LastName]
	, [FromTime]
	, [EndTime]
	, [Description]
	, [Doctor]
	, [ParentId]
	, [AppointmentType]
	, [CreatedDate]
    FROM
	[dbo].[Appointment]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([LocalId] = @LocalId AND @LocalId is not null)
	OR ([FirstName] = @FirstName AND @FirstName is not null)
	OR ([LastName] = @LastName AND @LastName is not null)
	OR ([FromTime] = @FromTime AND @FromTime is not null)
	OR ([EndTime] = @EndTime AND @EndTime is not null)
	OR ([Description] = @Description AND @Description is not null)
	OR ([Doctor] = @Doctor AND @Doctor is not null)
	OR ([ParentId] = @ParentId AND @ParentId is not null)
	OR ([AppointmentType] = @AppointmentType AND @AppointmentType is not null)
	OR ([CreatedDate] = @CreatedDate AND @CreatedDate is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
